package com.hua.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hua.pojo.Food;
import com.hua.pojo.FoodClass;
import com.hua.mapper.FoodClassMapper;
import com.hua.service.FoodClassService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hua.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hua
 * @since 2023-12-24
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class FoodClassServiceImpl extends ServiceImpl<FoodClassMapper, FoodClass> implements FoodClassService {


    //客户端
    //根据饭堂id获取菜品类型信息
    @Override
    public Map<String, Object> getFoodClassListById(int canteenId) {

        //多条件查询
        LambdaQueryWrapper<FoodClass> wrapper=new LambdaQueryWrapper<>();
        //添加查询条件
        wrapper.eq(FoodClass::getCanteenId,canteenId);//饭堂id

        //执行查询
        List<FoodClass> foodClassList = this.baseMapper.selectList(wrapper);
        List<FoodClass> specificIdItems = new ArrayList<>();
        for (FoodClass foodClass : foodClassList) {
            if (foodClass.getId() % 2 != 0 && foodClass.getId() > 0 && foodClass.getId() < 8) { // Check if id is 1, 3, 5, 7, etc.
                specificIdItems.add(foodClass);
            }
        }

        foodClassList.removeIf(specificIdItems::contains); // 在原来的列表去掉未分类的这项
        foodClassList.addAll(specificIdItems); // 在原来的末尾加上

        // 创建结果 Map 对象
        Map<String, Object> data = new HashMap<>();
        data.put("data", foodClassList);

        return data;
    }










    //管理端
    //管理端——①根据菜品类别id删除菜品类别信息  //这里是单个
    @Override
    public int deleteFoodClassByIds(String id) {
        // 执行批量删除操作
        Map<String, Object> data = new HashMap<>();

        // 根据菜品ID执行删除操作
        int deleted = this.baseMapper.deleteById(id);

        return deleted;
    }





    //管理端——②菜品类别信息的修改
    @Override
    public Map<String, Object> modifyFoodClass(FoodClass foodClass) {
        int i = this.baseMapper.updateById(foodClass);
        if(i==1){
            // 创建结果 Map 对象
            Map<String, Object> data = new HashMap<>();
            data.put("data", "修改成功");
            return data;
        }
        return null;
    }


    //管理端——③菜品类别的新增
//    @Override
//    public Map<String, Object> addFoodClass(FoodClass foodClass) {
//        int i = this.baseMapper.insert(foodClass);
//        if(i==1){
//            // 创建结果 Map 对象
//            Map<String, Object> data = new HashMap<>();
//            data.put("data", "新增成功");
//            return data;
//        }
//        return null;
//    }


    //管理端——③菜品类别的新增//批量处理
    @Override
    public Map<String, Object> addFoodClassList(List<FoodClass> foodClassList) {
        int successCount = 0;
        for (FoodClass foodClass : foodClassList) {
            int success = this.baseMapper.insert(foodClass);
            if (success==1) {
                successCount++;
            } else {
                throw new RuntimeException("新增失败，事务回滚！");
            }
        }

        if (successCount == foodClassList.size()) {
             //创建结果 Map 对象
            Map<String, Object> data = new HashMap<>();
            data.put("data", "新增成功");
            return data;
        } else {
            return null;
        }
    }




}
